home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Virtuals in constructor
- Date: Wed, 21 Feb 1996 01:50:03 GMT
- Organization: Netcom
- Message-ID: <312a779d.5728997@nntp.ix.netcom.com>
- References: <312A3A72.688D@scopus.ch>
- NNTP-Posting-Host: ix-dc13-30.ix.netcom.com
- X-NETCOM-Date: Tue Feb 20 5:50:15 PM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- Fabienne Guinnard <guinnard_f@scopus.ch> wrote:
-
- > Hi,
- >
- > does anyone know why the following code doesn't work ?
- >
- > Run-time error: pure virtual function called
- >
- > class A
- > {
- > public:
- > A(VOID) { Method(); }
- >
- > virtual VOID Method(VOID) = 0;
- > };
- >
- > class B : public A
- > {
- > public:
- > B(VOID) {}
- >
- > VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
- > };
- >
- > int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- > {
- > B b;
- >
- > return 0;
- > }
-
- Because you are calling a pure virtual function.
-
- When A::A() is invoked to construct the A part of b the B part does
- not yet exist. The call to Method() in A::A() therefore calls
- A::Method() which is not defined.
-
-
- Michael M Rubenstein
-